package com.morn.weibo.types;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.j256.ormlite.field.DatabaseField;
import com.morn.weibo.exception.WeiboException;
import com.morn.weibo.utils.DateUtils;

public class Message implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7882914029552725464L;

	@DatabaseField(id = true)
	private String id;

	@DatabaseField
	private String text;

	@DatabaseField(columnName = "senderId")
	private String sender_id;

	@DatabaseField(columnName = "recipientId")
	private String recipient_id;

	@DatabaseField
	private Date created_at;

	@DatabaseField
	private String sender_screen_name;

	@DatabaseField
	private String recipient_screen_name;

	@DatabaseField(foreign = true)
	private User sender;

	@DatabaseField(foreign = true)
	private User recipient;

	public Message() {

	}

	public Message(JSONObject json) throws WeiboException {
		init(json);
	}

	private void init(JSONObject json) throws WeiboException {
		if (json != null) {
			try {
				id = json.getString("id");
				text = json.getString("text");
				sender_id = json.getString("sender_id");
				recipient_id = json.getString("recipient_id");
				created_at = DateUtils.parseDate(json.getString("created_at"),
						"EEE MMM dd HH:mm:ss ZZZZZ yyyy");
				sender_screen_name = json.getString("sender_screen_name");
				recipient_screen_name = json.getString("recipient_screen_name");
				if (!json.isNull("sender")) {
					sender = new User(json.getJSONObject("sender"));
				}
				if (!json.isNull("recipient")) {
					recipient = new User(json.getJSONObject("recipient"));
				}
			} catch (JSONException e) {
				throw new WeiboException(
						e.getMessage() + ":" + json.toString(), e);
			}
		}
	}

	public static List<Message> getMessageList(JSONArray array)
			throws WeiboException, JSONException {
		int size = array.length();
		List<Message> list = new ArrayList<Message>();
		for (int i = 0; i < size; i++) {
			list.add(new Message(array.getJSONObject(i)));
		}
		return list;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getSender_id() {
		return sender_id;
	}

	public void setSender_id(String sender_id) {
		this.sender_id = sender_id;
	}

	public String getRecipient_id() {
		return recipient_id;
	}

	public void setRecipient_id(String recipient_id) {
		this.recipient_id = recipient_id;
	}

	public Date getCreated_at() {
		return created_at;
	}

	public void setCreated_at(Date created_at) {
		this.created_at = created_at;
	}

	public String getSender_screen_name() {
		return sender_screen_name;
	}

	public void setSender_screen_name(String sender_screen_name) {
		this.sender_screen_name = sender_screen_name;
	}

	public String getRecipient_screen_name() {
		return recipient_screen_name;
	}

	public void setRecipient_screen_name(String recipient_screen_name) {
		this.recipient_screen_name = recipient_screen_name;
	}

	public User getSender() {
		return sender;
	}

	public void setSender(User sender) {
		this.sender = sender;
	}

	public User getRecipient() {
		return recipient;
	}

	public void setRecipient(User recipient) {
		this.recipient = recipient;
	}

	@Override
	public String toString() {
		return "Message [id=" + id + ", text=" + text + ", sender_id="
				+ sender_id + ", recipient_id=" + recipient_id
				+ ", created_at=" + created_at + ", sender_screen_name="
				+ sender_screen_name + ", recipient_screen_name="
				+ recipient_screen_name + ", sender=" + sender + ", recipient="
				+ recipient + "]";
	}

	public static class ComparatorMessage implements Comparator<Message> {

		@Override
		public int compare(Message object1, Message object2) {
			Date date1 = object1.getCreated_at();
			Date date2 = object2.getCreated_at();
			return date1.compareTo(date2);
		}

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((created_at == null) ? 0 : created_at.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((recipient_id == null) ? 0 : recipient_id.hashCode());
		result = prime
				* result
				+ ((recipient_screen_name == null) ? 0 : recipient_screen_name
						.hashCode());
		result = prime * result
				+ ((sender_id == null) ? 0 : sender_id.hashCode());
		result = prime
				* result
				+ ((sender_screen_name == null) ? 0 : sender_screen_name
						.hashCode());
		result = prime * result + ((text == null) ? 0 : text.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Message other = (Message) obj;
		if (created_at == null) {
			if (other.created_at != null)
				return false;
		} else if (!created_at.equals(other.created_at))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (recipient_id == null) {
			if (other.recipient_id != null)
				return false;
		} else if (!recipient_id.equals(other.recipient_id))
			return false;
		if (recipient_screen_name == null) {
			if (other.recipient_screen_name != null)
				return false;
		} else if (!recipient_screen_name.equals(other.recipient_screen_name))
			return false;
		if (sender_id == null) {
			if (other.sender_id != null)
				return false;
		} else if (!sender_id.equals(other.sender_id))
			return false;
		if (sender_screen_name == null) {
			if (other.sender_screen_name != null)
				return false;
		} else if (!sender_screen_name.equals(other.sender_screen_name))
			return false;
		if (text == null) {
			if (other.text != null)
				return false;
		} else if (!text.equals(other.text))
			return false;
		return true;
	}

}
